// pages/withdrawals/withdrawals.js
import { request } from "../../utils/index.js";
import { request1 } from "../../utils/request.js";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:1, // 分页
    money:"", // 提现金额
    user_bank:"", // 收款账号
    list:[], // 提现记录列表
    userBankInfo:{}, // 收款账号信息
  },

  // 输入金额
  onChangeMoney(event){
    this.setData({money:event.detail})
  },

  // 选择收款账号
  onSelectUserBank(event) {

    // 修改收款账号
    if (this.data.userBankInfo.bank_num) { 
      let str=JSON.stringify(this.data.userBankInfo);
      wx.navigateTo({
        url: '/pages/createBank/createBank?type=1&jsonStr='+str, 
      })
    }else { // 绑定收款账号
      wx.navigateTo({
        url: '/pages/createBank/createBank?type=0',
      })
    }
  
  },

  // 查询收款账号
  getUserBank(){
    var that = this;
    request({ url: "/api/user_bank/index"})
    .then(result => {
      var bankList = result.data.list;
      if (bankList.length>0) {
        that.setData({
          userBankInfo:bankList[0],
          user_bank:bankList[0].bank_num
        })
      }
    })
  },

  // 查询提现记录数据
  getWithdrawalsLog(){
    var that = this;
    request1({ url: "/api/withdrawals/withdrawalsLog",data:{
      page:that.data.page,
    }})
    .then(result => {
      // console.log(result);
      that.setData({
        list:result.data.list
      })
    })
  },

  // 提现
  onSubmit(){
    var that = this;
    if(!that.data.money) {
      return wx.showToast({
        title: '请输入提现金额',
        icon: 'none',
        duration:2000
      });
    }

    if(!that.data.userBankInfo) {
      return wx.showToast({
        title: '请选择收款账号',
        icon: 'none',
        duration:2000
      });
    }

    request1({ url: "/api/withdrawals/add",data:{
      money:that.data.money,
      user_bank_id:that.data.userBankInfo.id
    }})
    .then(result => {
      wx.showToast({
        title: result.msg,
        icon: 'none',
        duration:2000
      });
      that.data.page = 1;
      that.data.list = [];
      that.getWithdrawalsLog();
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data.page = 1;
    this.data.list = [];
    this.getWithdrawalsLog();
    this.getUserBank();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})